

export default {
    data() {
        var checkPncodeInfo = (rule, value, callback) => {
            if (this.editType == 'add') {
                let para = {
                    "pncode": value
                }
                checkPncode(para).then(res => {
                    if (res.resultCode == constant.SUCCESS) {
                        if (res.data) {
                            console.log(res)
                            callback(new Error('输入的照会代码重复！'));
                        } else {
                            let CHNReg = /^[0-9a-zA-Z]*$/;//汉字的正则表达式
                            if (!value.match(CHNReg)) {
                                callback(new Error('照会代码必须为数字或者字母'));
                            } else {
                                callback();
                            }
                        }
                    } else {
                        callback(new Error('输入的照会代码重复！'));
                    }
                }).catch(err => {
                    callback(new Error('输入的照会代码重复!')); 
                })
            } else {
                callback();
            }


        }; 
        var validatePntype = (rule, value, callback) => {
            let para = {
                "pntype": value
            }

            checkPntype(para).then(res => {
                if (res.resultCode == constant.SUCCESS) {
                    if (res.data) {
                        console.log(res)
                        callback();
                    } else {
                        callback(new Error('照会类别不存在'));
                    }
                } else {
                    callback(new Error('照会类别不存在'));
                }
            }).catch(err => {
                callback(new Error('照会类别不存在'));
            })

        };
        var validateSrpisno = (rule, value, callback) => {
            let para = {
                "srpisno": value
            }

            if (value) {
                checkSrpisno(para).then(res => { 
                    if (res.resultCode == constant.SUCCESS) {
                        if (res.data == 0) {
                            callback(new Error('需求号码必须存在且尚未结案'));
                        } else {
                            console.log(res)
                            callback();
                        }
                    } else {
                        callback(new Error('需求号码必须存在且尚未结案'));
                    }
                }).catch(err => {
                    callback(new Error('需求号码必须存在且尚未结案')); 
                })
            } else {
                callback();
            }
        };
        var validateNofday = (rule, value, callback) => {
            if (isNaN(value) || value < 1 || value > 999 || value % 1 != 0) {
                callback(new Error('催办天数：请输入不超过三位数的正整数。'));
            } else {
                callback();
            }
        };
        var validateNofday = (rule, value, callback) => {
            if (isNaN(value) || value < 1 || value > 999 || value % 1 != 0) {
                callback(new Error('提醒日:请输入不超过三位数的正整数。'));
            } else {
                callback();
            }
        };
        return {
            //常量下拉框
            sources: [],
            forceinds: [
                {
                    value: 'Y',
                    label: '是'
                },
                {
                    value: 'N',
                    label: '否'
                }
            ],
            zresnpds: [],
            resinds: [{
                value: 'Y',
                label: '是'
            },
            {
                value: 'N',
                label: '否'
            }],
            prtopts: [
                {
                    value: 'Y',
                    label: '是'
                },
                {
                    value: 'N',
                    label: '否'
                }
            ],
            authnows: [
                {
                    value: 'Y',
                    label: '是'
                },
                {
                    value: 'N',
                    label: '否'
                }
            ],
            update_inds: [
                {
                    value: 'I',
                    label: 'I--新增'
                },
                {
                    value: 'U',
                    label: 'U--更新'
                },
                {
                    value: 'D',
                    label: 'D--删除'
                }
            ],
            replyCodeDialog: false,//默认隐藏 照会代码维护修改弹框
            // 查询条件
            rules: {
                pncode: [
                    { required: true, message: '请输入照会代码。' },
                    // { validator: validatePncode, trigger: 'blur' },
                    { validator: checkPncodeInfo, trigger: 'blur' }
                ],
                pntype: [
                    { required: true, message: '请输入照会类别。' },
                    { validator: validatePntype, trigger: 'blur' }
                ],
                source: [
                    { required: true, message: '请输入照会来源。' },
                ],
                forceind: [
                    { required: true, message: '请输入Force able。' },
                ],
                zresnpd: [
                    { required: true, message: '请输入照会性质。' },
                ],
                resind: [
                    { required: true, message: '请输入User Resol。' },
                ],
                nofday: [
                    { required: true, message: '请输入催办天数。' },
                    { validator: validateNofday, trigger: 'blur' }
                ],
                prtopt: [
                    { required: true, message: '请输入直接列印。' },
                ],
                authnow: [
                    { required: true, message: '请输入自动授权。' },
                ],
                remindday: [
                    { required: true, message: '请输入提醒日。' },
                    { validator: validateNofday, trigger: 'blur' }
                ],
                memodesc: [
                    { max: 50, trigger: 'blur' }
                ],
                pedesc: [
                    { required: true, message: '请输入照会说明。' },
                    { max: 256, trigger: 'change' }
                ]
            },
            searchParams: {
                pncode: '',
                pntype: '',
            },
            replyCodetableData: [//主档数据
            ],
            loading: false,
            editType: '', //编辑类型，add-新增 modify-修改
            form: { //弹框表单
                pncode: '',
                pntype: '',
                source: '',
                forceind: '',
                zresnpd: '',
                resind: '',
                nofday: '',
                prtopt: '',
                authnow: '',
                remindday: '',
                srpisno: '',
                updateInd: '',
                memodesc: '',
                pedesc: ''
            },
            option: '',
            options: [
                { value: '1', label: '测试1' },
                { value: '2', label: '测试2' }
            ],
            isCheck: true,
            saveButton: true,
            //需要给分页组件传的信息
            paginations: {
                total: 0,        // 总数
                pageIndex: 1,  // 当前位于哪页
                pageSize: 10,   // 1页显示多少条
                pageSizes: [5, 10, 15, 20],  //每页显示多少条
                layout: "total, sizes, prev, pager, next, jumper"   // 翻页属性
            },
            pickerOptions1: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                }
            }
        }
    },
    created() {
        //          this.getList();
    },
    mounted() {
        // this.getList();
        this.querySourceForNoteRule();
        this.queryZresnpdForNoteRule();
    },
    methods: {
        //校验需求号：
        checkSrp(value) {
            this.saveButton = false;
            if (value) {
                let para = {
                    "srpisno": value
                }
                checkSrpisno(para).then(res => {
                    if (res.resultCode == constant.SUCCESS) {
                        if (res.data == 0) {
                            this.isCheck = false;
                            this.$message({
                                message: '需求号码必须存在且尚未结案',
                                type: "error"
                            });
                        } else {
                            this.isCheck = true;
                        }
                    } else {
                        this.isCheck = false;
                        this.$message({
                            message: '需求号码必须存在且尚未结案',
                            type: "error"
                        });
                    }
                    this.saveButton = true;
                }).catch(err => {
                    this.isCheck = false;
                    this.$message({
                        message: '需求号码必须存在且尚未结案',
                        type: "error"
                    });
                    this.saveButton = true;
                })
            } else {
                this.isCheck = true;
                this.saveButton = true;
            }
        },
        //提交
        onSubmit(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                console.log(valid);
                if (!_this.isCheck) {
                    _this.$message({
                        message: '需求号码必须存在且尚未结案',
                        type: "error"
                    });
                    return;
                }
                if (valid) {
                    console.log('submit!!');
                    editData(_this.editType, _this.form).then(res => {
                        //this.$store.dispatch('loading/CHANGE_LOADING',false)
                        if (res.resultCode == constant.SUCCESS) {
                            _this.$message({
                                type: 'success',
                                message: res.resultMsg
                            });
                            _this.replyCodeDialog = false;
                            _this.paginations.pageIndex = 1;
                            _this.searchParams.pncode = _this.form.pncode;
                            _this.getList();
                        } else {
                            _this.$message({
                                type: 'error',
                                message: res.resultMsg
                            });
                        }
                    }).catch(err => {
                    })


                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //修改
        replyCodeUpdateGo(row) {
            this.replyCodeDialog = true;
            this.isCheck = true;
            this.saveButton = true;
            this.editType = 'modify';
            this.form = row;
        },
        //新增
        addEufakpay() {
            this.replyCodeDialog = true;
            this.editType = 'add';
            this.isCheck = true;
            this.saveButton = true;
            this.form = {
                pncode: '',
                pntype: '',
                source: 'F',
                forceind: 'Y',
                zresnpd: '01',
                resind: 'Y',
                nofday: '15',
                prtopt: 'Y',
                authnow: 'N',
                remindday: '15',
                srpisno: '',
                updateInd: '',
                memodesc: '',
                pedesc: ''
            };
        },
        deleteData(row) {
            this.$confirm('确定是否删除？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let param = {
                    pncode: row.pncode
                }
                deleteData(param).then(res => {
                    if (res.resultCode == constant.SUCCESS) {
                        this.$confirm("删除成功!", '提示', {
                            confirmButtonText: '确定',
                            type: 'warning'
                        }).then(() => {
                            this.paginations.pageIndex = 1;
                            this.getList();
                        })
                    }
                }).catch(err => {
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        changeToUpperCase(type) {
            if (type == 'pncode') {
                this.searchParams.pncode = this.searchParams.pncode.toUpperCase();
            } else if (type == 'pntype') {
                this.searchParams.pntype = this.searchParams.pntype.toUpperCase();
            }
        },
        // 查询列表
        getList() {
            this.paginations.total = 0;
            this.replyCodetableData = [];
            let para = {
                "pncode": this.searchParams.pncode,
                "pntype": this.searchParams.pntype,
                "pageSize": this.paginations.pageSize,
                "pageNumber": this.paginations.pageIndex,
            }
            this.$store.dispatch('loading/CHANGE_LOADING', true)
            getComCheckList(para).then(res => {
                this.$store.dispatch('loading/CHANGE_LOADING', false)
                if (res.resultCode == constant.SUCCESS && res.data) {
                    if (res.data.data.rows.length) {
                        this.replyCodetableData = res.data.data.rows;
                        this.paginations.total = res.data.data.total;
                    } else {
                        this.$message({
                            message: '无符合条件任务！',
                            type: 'error'
                        });
                    }

                }
            }).catch(err => {
                this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
        },
        // 每页多少条切换
        handleSizeChange(pageSize) {
            this.paginations.pageSize = pageSize;
            this.getList();
        },
        // 上下分页
        handleCurrentChange(page) {
            this.paginations.pageIndex = page;
            this.getList();
        },
        // 根据条件查询列表
        subGetList() {
            this.paginations.pageIndex = 1;
            this.getList();
        },
        // 清除查询条件
        resetSearch() {
            this.searchParams.pncode = '';
            this.searchParams.pntype = '';
        },
        //照会来源下拉框
        querySourceForNoteRule() {
            queryForNoteRule("18").then(res => {
                //this.$store.dispatch('loading/CHANGE_LOADING',false)
                if (res.resultCode == constant.SUCCESS && res.data && res.data.length) {
                    this.sources = res.data;
                }
            }).catch(err => {
                this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
        },
        //照会性质下拉框
        queryZresnpdForNoteRule() {
            queryForNoteRule("12").then(res => {
                //this.$store.dispatch('loading/CHANGE_LOADING',false)
                if (res.resultCode == constant.SUCCESS && res.data && res.data.length) {
                    this.zresnpds = res.data;
                }
            }).catch(err => {
                this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
        },
        //照会来源值转换
        sourceFormatter(row) {
            let result = "";
            this.sources.forEach(e => {
                if (e.value == row.source) {
                    result = e.option;
                    return false;
                }
            })
            return result;
        },
        forceindFormatter(row) {
            let result = "";
            this.forceinds.forEach(e => {
                if (e.value == row.forceind) {
                    result = e.label;
                    return false;
                }
            })
            return result;
        },
        zresnpdFormatter(row) {
            let result = "";
            this.zresnpds.forEach(e => {
                if (e.value == row.zresnpd) {
                    result = e.option;
                    return false;
                }
            })
            return result;
        },
        resindFormatter(row) {
            let result = "";
            this.resinds.forEach(e => {
                if (e.value == row.resind) {
                    result = e.label;
                    return false;
                }
            })
            return result;
        },
        prtoptFormatter(row) {
            let result = "";
            this.prtopts.forEach(e => {
                if (e.value == row.prtopt) {
                    result = e.label;
                    return false;
                }
            })
            return result;
        },
        authnowFormatter(row) {
            let result = "";
            this.authnows.forEach(e => {
                if (e.value == row.authnow) {
                    result = e.label;
                    return false;
                }
            })
            return result;
        },
        updateIndFormatter(row) {
            let result = "";
            this.update_inds.forEach(e => {
                if (e.value == row.updateInd) {
                    result = e.label;
                    return false;
                }
            })
            return result;
        }
    },
    computed: {
        ...mapGetters(['tags']),
        reload() {
            return this.$store.state.loading.reload;
        }
    },
    activated() {
        if (this.reload) {
            this.$store.dispatch('loading/CHNAGE_RELOAD', false)
            Object.assign(this.$data, this.$options.data());
        }
    }
}

